package com.crazy.base.common.aop.advice;

import com.crazy.base.common.core.dto.Result;
import com.crazy.base.common.core.exception.BaseException;
import com.crazy.base.common.core.exception.CommonExeceptionEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@Slf4j
@ControllerAdvice
@ResponseBody
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)
    public Result<?> handleException(Exception e) {
        log.error("未知异常：", e);
        return Result.of(BaseException.of(CommonExeceptionEnum.SERVER_ERROR));
    }

    @ExceptionHandler(IllegalArgumentException.class)
    public Result<?> handleIllegalArgumentException(IllegalArgumentException e) {
        log.error("参数错误：", e);
        return Result.of(BaseException.of(CommonExeceptionEnum.PARAM_ERROR));
    }

    @ExceptionHandler(BaseException.class)
    public Result<?> handleBaseException(BaseException e) {
        return Result.of(e);
    }
}
